package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyDiscountCouponAdapter;
import com.kkd.kkdapp.adapter.MyDiscountCouponAdapter.OnTenderItemtListener;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.MyDiscountCoupon;
import com.kkd.kkdapp.http.entity.MyDiscountCouponData;
import com.kkd.kkdapp.http.entity.MyDiscountCouponDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;

import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class WithdrawDiscountCounponActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private RelativeLayout regulations; // 使用规则
	private PopupWindow pop;
	private RelativeLayout deal_top_img; // 后退
	private RelativeLayout nonuse; // 暂不是用优惠券
	// private String version;
	private List<MyDiscountCouponDataList> mDataList;
	private MyDiscountCouponAdapter adapter;
	private KkdXlistView mListView;
	public final static String SER_KEY = "com.tutor.objecttran.ser";
	private int pageNO = 1;
	private int pageSize = 10;
	private boolean REFRESHFLAG = true;
	private List<MyDiscountCouponDataList> reDataList;
	private ImageView mNullImg;
	private TextView mNullTv;
	private Time_Date time_Date = new Time_Date();
	private String time;
	private int mPosition;
	private String mCouponId;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				mListView.stopLoadMore(-1);
				if (REFRESHFLAG) {
					mListView.stopLoadMore(1);
				}
				mListView.stopRefresh();
				break;
			case 2:
				if (reDataList.size() > 0) {
					mListView.setVisibility(View.VISIBLE);
					adapter.notifyDataSetChanged();
				} else {
					mListView.setVisibility(View.GONE);
				}
				if (reDataList.size() < pageSize) {
					mListView.stopLoadMore(1);
				} else {
					mListView.stopLoadMore(0);
				}
				mListView.stopRefresh();
				time = time_Date.get_Year_Day();
				mListView.setRefreshTime(time);
				break;
			case 3:
				if (REFRESHFLAG) {
					if (mDataList == null) {
						if (mDataList.size() == 0) {
							adapter.notifyDataSetChanged();
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
							mNullTv.setText("暂无提现券");
							// mListView.setPullLoadEnable(false);
						}
					} else {
						if (mDataList.size() == 0) {
							adapter.notifyDataSetChanged();
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
							mNullTv.setText("暂无提现券");
							// mListView.setPullLoadEnable(false);
						} else if (mDataList.size() > 0) {
							adapter.notifyDataSetChanged();
							// mListView.setPullLoadEnable(true);
							mNullImg.setVisibility(View.GONE);
							mNullTv.setVisibility(View.GONE);
						}
					}
				}
				break;
			}
		};
	};
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_withdraw_discount_counpon);
		initView();
		getMessage();
		setListener();
	}

	public void initView() {
		regulations = (RelativeLayout) this.findViewById(R.id.regulations);
		deal_top_img = (RelativeLayout) this.findViewById(R.id.deal_top_img);
		nonuse = (RelativeLayout) this.findViewById(R.id.nonuse);
		mListView = (KkdXlistView) this.findViewById(R.id.deal_detail_list);
		mNullImg = (ImageView) this.findViewById(R.id.id_no_content);
		mNullTv = (TextView) this.findViewById(R.id.id_no_content_text);
		if (mDataList == null) {
			mDataList = new ArrayList<MyDiscountCouponDataList>();
		}
		mListView.setKKDScrollListener(this);
		listView = mListView.mListView;
	}

	public void getMessage() {
		Intent intent = this.getIntent();
		mPosition = intent.getIntExtra("mId", -3);
		mCouponId = intent.getStringExtra("mCouponId");
		adapter = new MyDiscountCouponAdapter(this, true, mDataList, mPosition,
				mCouponId);
		listView.setAdapter(adapter);
		mListView.startRefresh();
	}

	public void setListener() {
		nonuse.setOnClickListener(this);
		regulations.setOnClickListener(this);
		deal_top_img.setOnClickListener(this);
		// mListView.setXListViewListener(this);
		// mListView.setPullRefreshEnable(true);
		// mListView.setPullLoadEnable(true);
		adapter.setOnTenderItemtListener(new OnTenderItemtListener() {

			@Override
			public void onPosition(int id) {
				Intent intent = new Intent();
				Bundle extras = new Bundle();
				MyDiscountCouponDataList mMyDiscountCouponDataList = mDataList
						.get(id);
				extras.putSerializable(SER_KEY, mMyDiscountCouponDataList);
				intent.putExtra("mId", id);
				intent.putExtras(extras);
				setResult(900, intent);
				finish();
			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.regulations: // 使用规则
			showPopup(v);
			break;
		case R.id.deal_top_img: // 后退
			finish();
			break;
		case R.id.nonuse: // 暂不使用优惠券
			adapter.mPosition = MyDiscountCouponAdapter.NOT_UESE;
			adapter.notifyDataSetChanged();
			setResult(800);
			finish();
			break;
		}
	}

	/**
	 * 使用规则
	 */
	public void showPopup(View v) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.regulations_popupwindow, null);
		pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT, false);
		pop.setOutsideTouchable(true);
		RelativeLayout close_tv = (RelativeLayout) view
				.findViewById(R.id.close_tv);
		pop.showAtLocation(v, 0, 0, 0);
		close_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				pop.dismiss();
				final WindowManager.LayoutParams attrs = getWindow()
						.getAttributes();
				attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
				getWindow().setAttributes(attrs);
				getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		couponList();
	}

	/**
	 * 优惠券
	 */
	public void couponList() {

		String coustomerIdAndToken = PreferenceUtils.getInstance()
				.getcustomerId(this);
		String[] cat = coustomerIdAndToken.split("&");
		if (cat == null || cat.length == 0)
			return;
		KkdHttpMagager.getInstance().getMyDiscountManager("", "", "2", "",
				 pageNO, pageSize, 0, 
				MyDiscountCouponData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyDiscountCouponData mMyDiscountCouponData = (MyDiscountCouponData) responseBody;
						int pageNO = mMyDiscountCouponData.getPageNO();
						int pageCount = mMyDiscountCouponData.getPageCount();
						reDataList = mMyDiscountCouponData.getDataList();
						if (pageNO > pageCount) {
							mHandler.sendEmptyMessage(1);
						} else {
							if (pageNO == 1) {
								mDataList.clear();
							}
							mDataList.addAll(reDataList);
							mHandler.sendEmptyMessage(2);
						}
						mHandler.sendEmptyMessage(3);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						mHandler.sendEmptyMessage(3);
						mHandler.postDelayed(new Runnable() {

							@Override
							public void run() {
								showToast("加载失败");
								mListView.stopLoadMore(0);
								mListView.stopRefresh();
								if (mDataList.size() == 0) {
									mNullImg.setVisibility(View.VISIBLE);
									mNullTv.setVisibility(View.VISIBLE);
									mNullTv.setText("暂无内容");
								}
							}
						}, 1000);
					}
				});

		// KkdHttpMagager.getInstance().getMyDiscountManager("", "", "2", "",
		// SystemUtil.getVersionName(this), cat[1], pageNO, pageSize, 0,
		// cat[0], MyDiscountCoupon.class, new HttpCallBackT() {
		//
		// @Override
		// public void onSuccess(int statusCode, Object res) {
		// MyDiscountCoupon mDiscountCoupon = (MyDiscountCoupon) res;
		// if (mDiscountCoupon.getCode() == 0) {
		// MyDiscountCouponData mMyDiscountCouponData = mDiscountCoupon
		// .getData();
		// int pageNO = mMyDiscountCouponData.getPageNO();
		// int pageCount = mMyDiscountCouponData
		// .getPageCount();
		// reDataList = mMyDiscountCouponData.getDataList();
		// if (pageNO > pageCount) {
		// mHandler.sendEmptyMessage(1);
		// } else {
		// if (pageNO == 1) {
		// mDataList.clear();
		// }
		// mDataList.addAll(reDataList);
		// mHandler.sendEmptyMessage(2);
		// }
		// } else {
		// Toasttool.MyToast(
		// WithdrawDiscountCounponActivity.this,
		// mDiscountCoupon.getDesc());
		// }
		// mHandler.sendEmptyMessage(3);
		// }
		//
		// @Override
		// public void onFailure(int statusCode, byte[] responseBody,
		// Throwable error) {
		// mHandler.sendEmptyMessage(3);
		// }
		// });
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		couponList();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		couponList();
	}

	// @Override
	// public void onRefresh() {
	// pageNO = 1;
	// REFRESHFLAG = true;
	// couponList();
	//
	// }
	//
	// @Override
	// public void onLoadMore() {
	// pageNO++;
	// REFRESHFLAG = false;
	// couponList();
	// }
}
